package com.qxl.forest.business.service.Implement;

import com.qxl.forest.business.service.UserService;
import com.qxl.forest.common.constants.RequestParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: qiuxinlin
 * @Dercription:
 * @Date: 14:40 2017/12/6
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    DataSource dataSource;

    @Override
    public List queryUser(RequestParams requestParams) {
        NamedParameterJdbcTemplate jbdcTemplate = new NamedParameterJdbcTemplate(dataSource);
        Map param = new HashMap<>();
        if (!StringUtils.isEmpty(requestParams.getName())) {
            param.put("name", requestParams.getName());
        }
        if (!StringUtils.isEmpty(requestParams.getDeptid())) {
            param.put("deptid", requestParams.getDeptid());
        }
        if (!StringUtils.isEmpty(requestParams.getBeginTime())) {
            param.put("beginTime", requestParams.getBeginTime());
        }
        if (!StringUtils.isEmpty(requestParams.getEndTime())) {
            param.put("endTime", requestParams.getEndTime());
        }
        List list = jbdcTemplate.queryForList("SELECT * FROM f_user u WHERE u.name LIKE " +
                        "CONCAT('%',:name,'%') AND " +
                        "(u.deptid = :deptid or u.deptid in ( select id from dept where pids like CONCAT('%[', :deptid, ']%') )) AND " +
                        "(u.createTime between CONCAT(:beginTime,' 00:00:00') and CONCAT(:endTime,' 23:59:59'))"
                , param);
        return list;
    }
}
